#!/usr/bin/env node

// 测试区块链服务的回退机制
const fs = require('fs');
const path = require('path');

// 模拟zkProofData
const mockZkProofData = {
  input: {
    preImage: "999999",
    dataHash: "21684337208779804888941250689604787706765813346243268687471433053195528470185"
  },
  proof: {
    A: ["123456789", "987654321"],
    B: ["123456789", "987654321"],
    C: ["123456789", "987654321"],
    Z: ["123456789", "987654321"],
    T1: ["123456789", "987654321"],
    T2: ["123456789", "987654321"],
    T3: ["123456789", "987654321"],
    Wxi: ["123456789", "987654321"],
    Wxiw: ["123456789", "987654321"],
    eval_a: "123456789",
    eval_b: "123456789",
    eval_c: "123456789",
    eval_s1: "123456789",
    eval_s2: "123456789",
    eval_zw: "123456789"
  }
};

// 测试函数
async function testBlockchainService() {
  console.log('🧪 测试区块链服务回退机制...\n');
  
  try {
    // 动态导入ES模块 (如果是ES模块)
    console.log('📦 导入区块链服务...');
    
    // 由于使用了ES模块，我们需要通过node来测试
    // 这里只是验证配置是否正确
    
    console.log('✅ 模拟数据准备完成:');
    console.log('  - PreImage:', mockZkProofData.input.preImage);
    console.log('  - DataHash:', mockZkProofData.input.dataHash);
    console.log('  - Proof字段数量:', Object.keys(mockZkProofData.proof).length);
    
    console.log('\n🔄 配置的Group IDs: [0, 1]');
    console.log('📝 预期行为:');
    console.log('  1. 尝试Group 0 → 失败 (group not exist)');
    console.log('  2. 尝试Group 1 → 失败 (group not exist)');
    console.log('  3. 检测到方法不存在 → 自动回退到模拟');
    console.log('  4. 返回模拟的成功交易');
    
    console.log('\n🎯 在前端应用中测试:');
    console.log('  1. 访问 http://localhost:3002/');
    console.log('  2. 点击紫色 # 按钮输入自定义数据');
    console.log('  3. 进入Stage 4并点击"发射到区块链"');
    console.log('  4. 观察控制台日志确认回退过程');
    
  } catch (error) {
    console.error('❌ 测试失败:', error.message);
  }
}

// 运行测试
testBlockchainService().then(() => {
  console.log('\n✅ 测试脚本完成!');
}).catch(console.error);